package cc.ruih.meizanchen;

import java.util.Calendar;

import cc.ruih.meizanchen.R;
import cc.ruih.meizanchen.bb.BBIndex;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.InputType;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Toast;

/**
 * 宝宝登录页面
 * @author 郭瑞坡
 *
 */
public class BBLogin extends Activity {

	Button confirm;
	EditText bbmz;
	EditText mmmz;
	EditText birth;	//宝宝生日
	Button boyBtn;		// 男孩默认为选中
	Button girlBtn;
	int sex = 1;	// 宝宝性别: 1.boy 0.girl
	
	private int mYear;
	private int mMonth;
	private int mDay;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bb_login);
		
		bbmz = (EditText) findViewById(R.id.bbmz);
		mmmz = (EditText) findViewById(R.id.mmmz2);
		birth = (EditText) findViewById(R.id.birth);
		confirm = (Button) findViewById(R.id.confirm2);
		boyBtn = (Button) findViewById(R.id.boy);
		girlBtn = (Button) findViewById(R.id.girl);
		
		// 默认出生日期
		final Calendar c = Calendar.getInstance(); 
		mYear=c.get(Calendar.YEAR);
		mMonth=c.get(Calendar.MONTH);
		mDay=c.get(Calendar.DAY_OF_MONTH);
		birth.setText(mYear+"年"+(mMonth+1)+"月"+mDay+"日");	
	    
		birth.setOnClickListener(new BirthClick());		// 选择生日
		birth.setInputType(InputType.TYPE_NULL);
		confirm.setOnClickListener(new ButtonClick());		// 点击确认按钮
		// 点击女孩
		girlBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkSex();
			}
		});
		// 点击男孩
		boyBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				checkSex();
			}
		});
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==KeyEvent.KEYCODE_BACK){
			Intent main=new Intent();
			main.setClass(getApplicationContext(), Main.class);		// 跳到入口页
			startActivity(main);
		}
		return super.onKeyDown(keyCode, event);
	}

	private void checkSex(){
		if(sex == 0){	// 如果当前性别为女孩,会把性别改为男
			boyBtn.setBackgroundResource(R.drawable.checked);
			girlBtn.setBackgroundResource(R.drawable.uncheck);
			sex=1;
		}else{
			girlBtn.setBackgroundResource(R.drawable.checked);
			boyBtn.setBackgroundResource(R.drawable.uncheck);
			sex=0;
		}
	}
	
	//创建DatePickerDialog
	protected Dialog createTimePickerDialog() {
		return new DatePickerDialog(this,
				mDateSetListener, mYear, mMonth, mDay);
	}
	
	//DatePickerDialog上的监听
	private DatePickerDialog.OnDateSetListener mDateSetListener =
        new DatePickerDialog.OnDateSetListener() {
            public void onDateSet(DatePicker view, int year, int month,
                    int dayOfMonth) {
            	birth.setText(new StringBuilder()
        	                    .append(year).append("年")
        	                    .append(month+1).append("月")
        	                    .append(dayOfMonth).append("日"));
            	mYear=year;
            	mMonth=month;
            	mDay=dayOfMonth;
            }
        };
	
    // 选择生日
	private class BirthClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			createTimePickerDialog().show();
		}
	}
	
	// 确认按钮
	private class ButtonClick implements OnClickListener {
		@Override
		public void onClick(View v) {
			if(mmmz.getText().toString() != null && mmmz.getText().toString() != "" 
					&& mmmz.getText().toString().length() > 0 && bbmz.getText().toString() != null 
					&& bbmz.getText().toString() != "" && bbmz.getText().toString().length() > 0
					&& birth.getText().toString().length() > 0 && birth.getText().toString() != null 
					&& bbmz.getText().toString().length() > 0) {
				// SharedPreferences存储宝宝信息
				SharedPreferences sharedPreferences = getSharedPreferences("bbinfo", Context.MODE_PRIVATE);
				Editor editor = sharedPreferences.edit();	//获取编辑器
				editor.putString("bbname", bbmz.getText().toString());
				editor.putString("mmname", mmmz.getText().toString());
				editor.putString("birth", birth.getText().toString());
				editor.putInt("sex", sex);
				editor.commit();
				
				Intent intent = new Intent();
				intent.setClass(BBLogin.this, BBIndex.class);	// 跳转到孕妈妈首页
				startActivity(intent);
			}else{
				Toast.makeText(getApplicationContext(), "输入字段不能为空", Toast.LENGTH_LONG).show();
				return;
			}
		}
    }
}
